import java.awt.Color;
import java.util.LinkedList;


public class Snake {
	protected final int move_step;
	protected final int size_step;
	public final Color color;
	protected int sz;
	protected MyPoint head;
	protected LinkedList<MyPoint> pt;
	//TODO put head in pt

	/**
	 * Creates a snake
	 * @param iX Initial X coordinate
	 * @param iY Initial Y coordinate
	 * @param ms Move step
	 * @param ss Size step
	 * @param fi First size
	 * @param col Line color
	 */
	public Snake(int iX, int iY, int ms, int ss, int fi, Color col) {
		pt = new LinkedList<MyPoint>();
		pt.add(new MyPoint(iX, iY, 0));
		color = col;
		move_step = ms;
		size_step = ss;
		head = pt.getFirst().compute(sz = fi);
	}
	
	/**
	 * Sets a new direction for the head
	 * @param angle Direction in radians
	 */
	public void changeDirection(double angle) {
		MyPoint nw = new MyPoint((int)head.getX(), (int)head.getY(), angle);
		if (angle!=head.getAngle() && angle!=(head.getAngle()+Math.PI)%(2*Math.PI)) {
			pt.add(nw);
			head = nw;
		}
	}
	
	/**
	 * Moves the head
	 */
	protected void updateSize() {
		sz += size_step;
		head = head.compute(size_step);
	}
	
	/**
	 * Moves the whole snake
	 */
	protected void updatePosition() {
		//Move the tail
		pt.addFirst(pt.removeFirst().compute(move_step));
		
		//Merge if needed
		if (pt.size()>1 && pt.peekFirst().equals(pt.get(1)))
			pt.removeFirst();
		
		//Move the head
		head = head.compute(move_step);
	}

}
